# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=doxygen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.9.3
pkgrel=1
pkgdesc="A documentation system for C++, C, Java, IDL and PHP (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.doxygen.nl/"
options=('strip' 'staticlibs')
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         #"${MINGW_PACKAGE_PREFIX}-clang" # we dont want this huge dependency
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xapian-core")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             #"${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             #"${MINGW_PACKAGE_PREFIX}-polly"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt5-base")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/doxygen/doxygen/archive/Release_${pkgver//./_}.tar.gz
        cmake-mingw.patch
        "0003-remove-git-version-from-bins.patch")
sha256sums=('c29426222c9361dc33b762cf1c6447c78cfb0b9c213e5dcdbe31a10540c918c5'
            '7a74cdd4cfaba3f5521b5179a91eec4ad96c1da89534576be356238809df4aa6'
            'a57e2e680b0a01496d4f84e275e1095a9f092ede6903c63524f15d28884dd0f5')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-Release_${pkgver//./_}"

  apply_patch_with_msg \
    cmake-mingw.patch \
    0003-remove-git-version-from-bins.patch
}

build() {
  export QTDIR=${MINGW_PREFIX}

  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -Dbuild_wizard=ON \
    -Dbuild_search=ON \
    -Duse_sqlite3=ON \
    -Duse_libclang=OFF \
    -Dbuild_parse=ON \
    ../${_realname}-Release_${pkgver//./_}

  # fix some generated makefiles: replace LLVM-NOTFOUND by the path to LLVM.dll
  # llvm_path_mixed=$(cygpath -m `which libLLVM.dll`)
  # llvm_path_unix=$(echo ${llvm_path_mixed} | sed 's|C:|/C|')
  # sed -e "s|\(bin/.*\.exe: \)LLVM-NOTFOUND|\1${llvm_path_mixed}|" \
      # -e "s|\(.*libclangBasic.a \)LLVM-NOTFOUND|\1${llvm_path_unix}|" \
      # -i ./addon/doxyparse/CMakeFiles/doxyparse.dir/build.make \
         # ./src/CMakeFiles/doxygen.dir/build.make

  cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" cmake --install .
}
